package com.todo.user.application.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.utils.StringUtils;
import com.todo.component.redis.RedisKey;
import com.todo.component.redis.RedisService;
import com.todo.component.redis.generator.UserKeyGenerator;
import com.todo.user.application.convert.UserServiceConvert;
import com.todo.user.application.entity.bo.UserAccountBO;
import com.todo.user.application.entity.bo.UserInfoDetailBO;
import com.todo.user.application.service.UserCacheService;
import com.todo.user.infrastructure.entity.po.UserAccountPO;
import com.todo.user.infrastructure.repository.UserAccountRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zhangwenxiang
 * @date 2024/4/7 14:04
 * @description
 */
@Slf4j
@Service
public class UserCacheServiceImpl implements UserCacheService {

    @Resource
    private RedisService redisService;
    @Resource
    private UserAccountRepository userAccountRepository;

    @Override
    public void removeUserCache(String userName) {
        String userAccountInfoKey = UserKeyGenerator.getUserAccountInfoKey(userName);
        redisService.del(userAccountInfoKey);
        log.info("UserCacheService#removeUserCache 删除用户缓存完成 key：【{}】", userAccountInfoKey);
    }

    @Override
    public UserAccountBO getUserAccount(String userName) {
        if (StringUtils.isBlank(userName)) {
            return null;
        }
        String userAccountInfoKey = UserKeyGenerator.getUserAccountInfoKey(userName);
        String value = redisService.get(userAccountInfoKey);
        UserAccountBO cacheUserAccountBO = JSONObject.parseObject(value, UserAccountBO.class);
        if (cacheUserAccountBO == null) {
            log.info("UserCacheService#getUserAccount 查询缓存数据为空，默认查询数据库并填充缓存 userName：【{}】", userName);
            UserAccountPO userAccountPO = userAccountRepository.queryUserAccountPOByUserName(userName);
            if (userAccountPO == null) {
                log.warn("UserCacheService#getUserAccount 数据查询为空，默认返回空 userName：【{}】", userName);
                return null;
            }
            UserAccountBO userAccountBO = UserServiceConvert.convertToUserAccountBO(userAccountPO);
            setUserAccount(userAccountBO);
            return userAccountBO;
        }
        return cacheUserAccountBO;
    }

    @Override
    public void setUserAccount(UserAccountBO userAccountBO) {
        if (userAccountBO == null || userAccountBO.getId() == null) {
            return;
        }
        String userAccountInfoKey = UserKeyGenerator.getUserAccountInfoKey(userAccountBO.getUserName());
        redisService.set(userAccountInfoKey, JSONObject.toJSONString(userAccountBO), RedisKey.HALF_DAY_TIME);
    }

    @Override
    public UserInfoDetailBO getUserInfoDetail(Long userId) {
        if (userId == null || userId < 0) {
            return null;
        }
        String userDetailKey = UserKeyGenerator.getUserDetailKey(userId);
        String value = redisService.get(userDetailKey);
        return JSONObject.parseObject(value, UserInfoDetailBO.class);
    }

    @Override
    public void setUserInfoDetail(UserInfoDetailBO userInfoDetailBO) {
        if (userInfoDetailBO == null || userInfoDetailBO.getUserId() == null) {
            return;
        }
        String userDetailKey = UserKeyGenerator.getUserDetailKey(userInfoDetailBO.getUserId());
        redisService.set(userDetailKey, JSONObject.toJSONString(userInfoDetailBO), RedisKey.HALF_DAY_TIME);
    }

    @Override
    public void removeUserInfoDetail(Long userId) {
        if (userId == null || userId < 0) {
            return;
        }
        String userDetailKey = UserKeyGenerator.getUserDetailKey(userId);
        redisService.del(userDetailKey);
    }

    @Override
    public void setAuthCode(String telephone, String value) {
        if (StringUtils.isBlank(telephone) || StringUtils.isBlank(value)){
            return;
        }
        String authCodeKey = UserKeyGenerator.getAuthCodeKey(telephone);
        //默认10min
        redisService.set(authCodeKey,value,RedisKey.ONE_MINUTES_TIME * 10);
    }

    @Override
    public String getAuthCode(String telephone) {
        String authCodeKey = UserKeyGenerator.getAuthCodeKey(telephone);
        return redisService.get(authCodeKey);
    }

}
